<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="list"></home-swiper>
    <home-icons :list="icons"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import { mapState } from 'vuex'
import axios from 'axios'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data: function () {
    return {
      list: [],
      icons: [],
      recommendList: [],
      weekendList: [],
      lastCity: ''
    }
  },
  computed: {
    ...mapState(['city'])
  },
  mounted: function () { // 挂载完毕立即获取后端数据
    this.getInfo()
  },
  activated: function () { // 当页面重新显示时执行,如果城市改变再次发请求，不变化就不发
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getInfo()
    }
  },
  methods: {
    getInfo: function () {
      axios.get('/api/index.json?city=' + this.city)// 针对城市变化发送不同的请求参数
        .then(this.handleInfo)
    },
    handleInfo: function (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.list = data.swiperList
        this.icons = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  }
}
</script>

<style>
</style>
